package com.zhijiaoyun.common;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.Random;

import org.springframework.web.multipart.MultipartFile;

import com.zhijiaoyun.aliyun.oss.OSSManager;

/**
 * @author shisfish
 * @date 2017/6/6
 */
public class FileUtils {

	public static String saveFile(MultipartFile file) {
		String filePath = "";
		try {
			if (file != null) {
				// 取得当前上传文件的文件名称
				String myFileName = file.getOriginalFilename();
				// 如果名称不为“”,说明该文件存在，否则说明该文件不存在
				if (StringUtils.isNotEmptyString(myFileName)) {
					Date now = new Date();
					String normalFilePath = CommonUtils.getProperty("normalFilePath");
					// 定义上传路径
					CommonUtils.checkPath(normalFilePath);
					String extension = myFileName.substring(myFileName.lastIndexOf(".")).toLowerCase();
					String filename = DateUtils.getDateFormat(now, "yyyyMMddhhmmssSSS") + "_"
							+ new Random().nextInt(100) + extension;
					System.out.println(normalFilePath + filename);
					File localOriginFile = new File(normalFilePath + filename);
					file.transferTo(localOriginFile);
					filePath = CommonUtils.getProperty("urlPath") + filename;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		return filePath;
	}

	public static void  downLoadFromUrl(String urlStr,String fileName, String savePath) throws IOException {
		URL url = new URL(urlStr);
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		//设置超时间为3秒
		conn.setConnectTimeout(3*1000);
		//防止屏蔽程序抓取而返回403错误
		conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

		//得到输入流
		InputStream inputStream = conn.getInputStream();
		//获取自己数组
		byte[] getData = readInputStream(inputStream);
		File file = new File(savePath + fileName);
		FileOutputStream fos = new FileOutputStream(file);
		fos.write(getData);
		if(fos!=null){
			fos.close();
		}
		if(inputStream!=null){
			inputStream.close();
		}

	}

	public static  byte[] readInputStream(InputStream inputStream) throws IOException {
		byte[] buffer = new byte[1024];
		int len = 0;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		while((len = inputStream.read(buffer)) != -1) {
			bos.write(buffer, 0, len);
		}
		bos.close();
		return bos.toByteArray();
	}

	public static String saveCoursePicture(MultipartFile file) {
		String saveDir = CommonUtils.getProperty("course_picture_path");
		return saveFile(file, saveDir);
	}

	public static String saveNewsPicture(MultipartFile file) {
		String saveDir = CommonUtils.getProperty("news_picture_path");
		return saveFile(file, saveDir);
	}

	public static String saveBannerPicture(MultipartFile file) {
		String saveDir = CommonUtils.getProperty("banner_picture_path");
		return saveFile(file, saveDir);
	}

	public static String saveUserHeadImg(MultipartFile file) {
		String saveDir = CommonUtils.getProperty("user_headimg_path");
		return saveFile(file, saveDir);
	}


	public static String saveFile(MultipartFile file, String saveDir) {
		String filePath = "";
		if (file != null) {
			String myFileName = file.getOriginalFilename();
			if (StringUtils.isNotEmptyString(myFileName)) {
				String normalFilePath = CommonUtils.getProperty("normalFilePath");
				CommonUtils.checkPath(normalFilePath);
				String extension = myFileName.substring(myFileName.lastIndexOf(".")).toLowerCase();
				String filename = DateUtils.getDateFormat(new Date(), "yyyyMMddhhmmssSSS") + "_"
						+ new Random().nextInt(100) + extension;
				File tempFile = new File(normalFilePath + filename);
				try {
					file.transferTo(tempFile);
					filePath = OSSManager.updateFile(saveDir + filename, tempFile, saveDir, filename);
					tempFile.delete();
					return filePath;
				} catch (IOException e) {
					e.printStackTrace();
					return "";
				}
			}
		}
		return filePath;
	}

}
